#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time      :2021/3/25 16:43
# @Author    :cjw
import requests
from lxml import etree


def get_info(search_info: str = 'saas'):
    """获取猪八戒网站信息"""
    url = 'https://beijing.zbj.com/search/f/'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
    }
    data = {'kw': search_info}
    resp = requests.get(url, params=data, headers=headers)
    html = etree.HTML(resp.text)
    # 拿到所有服务商的div
    div_list = html.xpath('//div[@class="new-service-wrap"]/div')
    print(len(div_list))
    for div in div_list:
        # .//从当前div节点的相对路径中查找
        price = _[0].lstrip('¥') if (_ := div.xpath(
            './/div[@class="service-price clearfix"]/span[@class="price"]/text()')) != [] else None
        amount = _[0].rstrip('笔成交') if (_ := div.xpath(
            './/div[@class="service-price clearfix"]/span[@class="amount"]/text()')) != [] else None
        server = 'SAAS'.join(div.xpath('.//div[@class="service-title"]/p/text()'))
        name = _[0] if (_ := div.xpath('.//div[@class="service-shop clearfix"]/p/text()')) != [] else None
        city = _[0] if (_ := div.xpath('.//div[@class="service-shop clearfix"]/div/span/text()')) != [] else None
        print(name, server, price, amount, city)
    resp.close()


if __name__ == '__main__':
    get_info()
